﻿// ———————————————————————–
// <copyright file="ValueScheme.cs" company="The MITRE Corporation">
//    Copyright (c) 2010 The MITRE Corporation. All rights reserved.
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//    http://www.apache.org/licenses/LICENSE-2.0
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
// </copyright>
// ———————————————————————–
/////////////////////////////////////////////////////////////////////////////////////////////////
// ValueScheme.cs - Library Class for the Value Scheme Data Type; The identifier of an explicit recipient
// Project: IC.NET - EDXL Library
//
// Language:    C#, .NET 4.0
// Platform:    Windows 7, VS 2010 Beta 2
// Author:      Don McGarry MITRE Rome

/////////////////////////////////////////////////////////////////////////////////////////////////

// Copyright (c) 2010 The MITRE Corporation. All rights reserved.
//
// NOTICE
// This software was produced for the U. S. Government
// under Contract No. FA8721-09-C-0001, and is
// subject to the Rights in Noncommercial Computer Software
// and Noncommercial Computer Software Documentation Clause
// (DFARS) 252.227-7014 (JUN 1995)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Serialization;

namespace EDXLSharp.EDXLDELib
{
  /// <summary>
  /// The identifier of an explicit recipient
  /// </summary>
  [Serializable]
  public partial class ValueScheme : IDEComposableObject
  {
    #region Private Member Variables

    /// <summary>
    /// Identifies the distribution addressing scheme used. 
    /// </summary>
    private string explicitAddressScheme;

    /// <summary>
    /// A properly formed -escaped if necessary- XML string denoting the addressees value. 
    /// </summary>
    private List<string> explicitAddressValue;

    #endregion

    #region Constructors

    /// <summary>
    /// Initializes a new instance of the ValueScheme class
    /// Default Ctor - Initializes List
    /// </summary>
    public ValueScheme()
    {
      this.explicitAddressValue = new List<string>();
    }

    #endregion

    #region Public Accessors

    /// <summary>
    /// Gets or sets
    /// /Examples for this type of distribution includes - email, military USMTF, etc. . .
    /// </summary>
    [XmlElement(ElementName = "explicitAddressScheme", Order = 0)]
    public string ExplicitAddressScheme
    {
      get { return this.explicitAddressScheme; }
      set { this.explicitAddressScheme = value; }
    }

    /// <summary>
    /// Gets or sets
    /// A properly formed -escaped if necessary- XML string denoting the addressees val.
    /// </summary>
    [XmlIgnore]
    public List<string> ExplicitAddressValue
    {
      get { return this.explicitAddressValue; }
      set { this.explicitAddressValue = value; }
    }

    /// <summary>
    /// Gets or sets
    /// XML Serialization Object for Explicit Address Value
    /// </summary>
    [XmlElement(ElementName = "explicitAddressValue", Order = 1)]
    public string[] ExplicitAddressValueXML
    {
      get
      {
        if (this.explicitAddressValue.Count == 0)
        {
          return null;
        }
        else
        {
          return this.explicitAddressValue.ToArray();
        }
      }

      set
      {
        if (value != null)
        {
          this.explicitAddressValue = value.ToList();
        }
      }
    }

    #endregion

    #region Public Member Functions

    /// <summary>
    /// Writes This Object into an XML Document
    /// </summary>
    /// <param name="xwriter">Xml Writer for an Existing XML Docment</param>
    internal override void WriteXML(XmlWriter xwriter)
    {
      this.Validate();
      xwriter.WriteElementString("explicitAddressScheme", this.explicitAddressScheme);
      foreach (string value in this.explicitAddressValue)
      {
        if (String.IsNullOrEmpty(value))
        {
          continue;
        }

        xwriter.WriteElementString("explicitAddressValue", value);
      }

      xwriter.Flush();
    }

    /// <summary>
    /// Reads this Object from XML
    /// </summary>
    /// <param name="rootnode">Exising XML Node</param>
    internal override void ReadXML(XmlNode rootnode)
    {
      foreach (XmlNode node in rootnode.ChildNodes)
      {
        if (String.IsNullOrEmpty(node.InnerText))
        {
          continue;
        }

        switch (node.LocalName)
        {
          case "explicitAddressScheme":
            this.explicitAddressScheme = node.InnerText;
            break;
          case "explicitAddressValue":
            this.explicitAddressValue.Add(node.InnerText);
            break;
          case "#comment":
            break;
          default:
            throw new FormatException("Invalid value: " + node.Name + "found in ValueScheme Type");
        }
      }

      this.Validate();
    }
    #endregion

    #region Private Member Functions

    /// <summary>
    /// Validates This Message Element For Required Values and Conformance
    /// </summary>
    protected override void Validate()
    {
      if (String.IsNullOrEmpty(this.explicitAddressScheme))
      {
        throw new ArgumentException("ExplicitAddressScheme Can't Be Null or Empty!");
      }
    }

    #endregion
  }
}
